package com.fingard.dsp.bank.directbank.lianlianpay01;


import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.Calendar;

public class LianLianPay01Transaction extends LianLianPay01Base implements ITransaction {

    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然日循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                String[] fileNameArr=getBankConfig().getValueAsString("downloadFileType").split(",");
                for(String fileName:fileNameArr){
                    //region 读取对账单文件
                    String filePath = transDownloadFile +p_req7516.actNum+File.separator
                            + Format.Date8Format.format(tmpCalendarFrom.getTime()) + File.separator
                            + fileName+"_"+p_req7516.actNum + "_" + Format.Date8Format.format(tmpCalendarFrom.getTime())+".txt";
                    WriteBankLogLn("开始读取文件：" + filePath);
                    String fileContent = FileHelper.readFileText(filePath,getCharset());
                    if(StringHelper.hasAnyChar(fileContent)){
                        WriteBankLogLn2("对账单重组后响应内容为:\r\n" + fileContent);
                        String[] totalLine = fileContent.replaceAll("\r","").split("\n");
                        for (String eachline : totalLine) {
                            //0.商户订单号,1.商户号,2.商户订单时间,3.商户业务编号,4.银通订单号,5.银通账务日期,6.订单金额,7.商户收付款标志(0收款 1付款)
                            //,8.交易状态(0-成功 5-退款),9.更新时间,10.手续费,11.支付产品,12.支付方式,13.订单信息,14.收款方银行,15.收款方账号,16.收款方名称
                            String[] eachNode = eachline.split(",");
                            if (eachNode.length > 13 && getValueByIndex(eachNode,2).length()>6) {
                                if(p_req7516.actNum.equals(getValueByIndex(eachNode,1))){
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.cur=p_req7516.actCur;
                                    tmpRetDetail.askNumber = getValueByIndex(eachNode,0);
                                    String tranDate=getValueByIndex(eachNode,2);
                                    if(StringHelper.hasAnyChar(tranDate) && tranDate.length()>8){
                                        tmpRetDetail.tranDate = tranDate.substring(0, 8);
                                    }else {
                                        tmpRetDetail.tranDate =tranDate;
                                    }
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    tmpRetDetail.tranTime = getValueByIndex(eachNode,2);
                                    String tranAmt=getValueByIndex(eachNode,6);
                                    /**0收 1付*/
                                    String cdFlag=getValueByIndex(eachNode,7);
                                    if(tranAmt.startsWith("-")){
                                        /**借贷标志*/
                                        if ("0".equals(cdFlag)) {
                                            tmpRetDetail.cdFlag = "1";
                                        } else if ("1".equals(cdFlag)) {
                                            tmpRetDetail.cdFlag = "2";
                                        }
                                        /**交易金额*/
                                        tmpRetDetail.tranAmt =tranAmt.substring(1);
                                    }else {
                                        /**借贷标志*/
                                        if ("0".equals(cdFlag)) {
                                            tmpRetDetail.cdFlag = "2";
                                        } else if ("1".equals(cdFlag)) {
                                            tmpRetDetail.cdFlag = "1";
                                        }
                                        /**交易金额*/
                                        tmpRetDetail.tranAmt =tranAmt;
                                    }
                                    tmpRetDetail.originator = getValueByIndex(eachNode,4);
                                    tmpRetDetail.bnkSeqNo=getValueByIndex(eachNode,4);
                                    tmpRetDetail.fee = getValueByIndex(eachNode,10);
                                    tmpRetDetail.usage=getValueByIndex(eachNode,13);
                                    tmpRetDetail.recAccBank = getValueByIndex(eachNode,14);
                                    tmpRetDetail.recAccNum = getValueByIndex(eachNode,15);
                                    tmpRetDetail.recAccName = getValueByIndex(eachNode,16);
                                    retAts.writeRetDetail(tmpRetDetail);
                                }else {
                                    WriteBankLogLn("其他付款账户明细数据，忽略");
                                }
                            }
                        }
                    }else {
                        WriteBankLogLn("没有对应的对账单文件");
                    }
                    //endregion
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DATE, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        return null;
    }
}
